package com.alex.xmlyfm.widgets;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.support.v4.content.ContextCompat;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alex.xmlyfm.R;

/**
 * Created by
 * Author: alex
 * Email:
 * Date: 15-10-22.
 */

/**
 * 用于发现模块 推荐栏目中 精品听单的条目展现
 */
public class SpecialItemView extends RelativeLayout {

    /**
     * 听单的图标
     */
    private ImageView imgIcon;

    private TextView txtTitle;

    private TextView txtSubtitle;
    private TextView txtNumber;
    private ImageView imgArrow;
    private ImageView imgLine;

    public ImageView getImgIcon() {
        return imgIcon;
    }

    /**
     * 在代码中，使用 new SpecialItemView 的时候
     * @param context
     */
    public SpecialItemView(Context context) {
        super(context);
        init(context,null);
    }

    /**
     * 在布局 xml 中 使用控件时候，使用的构造
     * @param context
     * @param attrs
     */
    public SpecialItemView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    private void init(Context context,AttributeSet attrs){
        // 初始化控件

        imgIcon = new ImageView(context);

        // 手写属性
        // LayoutParams 代表布局中android:layout_xxx 属性
        RelativeLayout.LayoutParams lp = new LayoutParams(
                120,
                120
        );

        lp.addRule(CENTER_VERTICAL);
        imgIcon.setLayoutParams(lp);
        imgIcon.setId(R.id.special_item_icon);
        imgIcon.setImageResource(R.mipmap.ic_launcher);
        addView(imgIcon);




        txtTitle = new TextView(context);
        lp = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);

        // 设置 textView 与 ImageView 顶部对齐
        lp.addRule(ALIGN_TOP,R.id.special_item_icon);
        lp.addRule(RIGHT_OF, R.id.special_item_icon);
        lp.leftMargin = 20; // 单位像素

        txtTitle.setLayoutParams(lp);
        txtTitle.setText("标题");

        txtTitle.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20);
        txtTitle.setTextColor(Color.BLACK);
        txtTitle.setId(R.id.special_item_title);

        addView(txtTitle);




        //
        txtSubtitle = new TextView(context);
        lp = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        lp.addRule(CENTER_VERTICAL);
        lp.addRule(ALIGN_LEFT, R.id.special_item_title);

        txtSubtitle.setLayoutParams(lp);

        txtSubtitle.setText("subTitle");
        txtSubtitle.setSingleLine();

        addView(txtSubtitle);




        //-------
        txtNumber = new TextView(context);

        lp = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);

        // 设置对齐，与图片底部对齐 与标题左对齐
        lp.addRule(ALIGN_BOTTOM,R.id.special_item_icon);
        lp.addRule(ALIGN_LEFT, R.id.special_item_title);
        txtNumber.setLayoutParams(lp);

        txtNumber.setText("10首声音");
        // 给 TextView 设置左侧图标  可以同时设置 左 上 友 下 的图标 图标必须经过 setBounds()
        txtNumber.setId(R.id.special_item_number);

        // 使用 v4 包获取 Drawable
        Drawable drawable = ContextCompat.getDrawable(context, R.mipmap.finding_album_img);
        drawable.setBounds(0, 0, 20, 20);
        txtNumber.setCompoundDrawables(drawable, null, null, null);
        addView(txtNumber);



        imgArrow = new ImageView(context);
        lp = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        imgArrow.setImageResource(R.mipmap.arrow_right);
        lp.addRule(ALIGN_PARENT_RIGHT);
        lp.addRule(CENTER_VERTICAL);
        imgArrow.setLayoutParams(lp);
        addView(imgArrow);



        imgLine = new ImageView(context);
        // 一个px高度的图片
        lp = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,2);

        imgLine.setLayoutParams(lp);
        lp.addRule(ALIGN_LEFT,R.id.special_item_title);
        lp.addRule(ALIGN_PARENT_RIGHT);
        lp.addRule(BELOW,R.id.special_item_number);

        lp.setMargins(0,5,0,0);
        imgLine.setImageResource(R.drawable.shape_line);
        addView(imgLine);

    }


    public void setTitle(String str){
//        if (str != null) {
//            str = "";
//        }

        txtTitle.setText(str);
    }


    public void setSubtitle(String subtitle){
        txtSubtitle.setText(subtitle);
    }


    public void setNumber(String number){
        txtNumber.setText(number);
    }

    public void setShowBottomLine(boolean show){
        if (show){
            imgLine.setVisibility(VISIBLE);
        }else{
            imgLine.setVisibility(INVISIBLE);
        }
    }

    public void setRightArrowResource(int resId){
        imgArrow.setImageResource(resId);
    }

}
